package edu.stanford.bmir.protege.web.client.ui.ontology.stkos.mapper.common;

import com.google.common.base.Optional;
import com.google.gwt.core.shared.GWT;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Focusable;
import com.google.gwt.user.client.ui.Widget;
import com.gwtext.client.core.EventObject;
import com.gwtext.client.widgets.menu.BaseItem;
import com.gwtext.client.widgets.tree.TreeNode;

import edu.stanford.bmir.protege.web.client.project.Project;
import edu.stanford.bmir.protege.web.client.rpc.stkosutil.StkosUtilServiceManager;
import edu.stanford.bmir.protege.web.client.rpc.stkosutil.common.UtilConstants;
import edu.stanford.bmir.protege.web.client.rpc.stkosutil.common.UtilEntityType;
import edu.stanford.bmir.protege.web.client.ui.library.dlg.DialogButton;
import edu.stanford.bmir.protege.web.client.ui.library.dlg.WebProtegeDialog;
import edu.stanford.bmir.protege.web.client.ui.library.dlg.WebProtegeDialogButtonHandler;
import edu.stanford.bmir.protege.web.client.ui.library.dlg.WebProtegeDialogCloser;
import edu.stanford.bmir.protege.web.client.ui.library.dlg.WebProtegeOKCancelDialogController;
import edu.stanford.bmir.protege.web.client.ui.library.msgbox.MessageBox;
import edu.stanford.bmir.protege.web.client.ui.library.text.ExpandingTextBox;
import edu.stanford.bmir.protege.web.client.ui.ontology.stkos.common.treepanel.AbstractStkosTreePanel;
import edu.stanford.bmir.protege.web.shared.project.ProjectId;

/**
 * Stkos Mapper Modify Mapper Number MenuItem.
 * 
 * @author LWS
 * 
 */
public class StkosMapperModifyMapperNumberMenuItem extends
		AbstractStkosMapperMenuItem {

	public static final String TEXT = "修改映射号";
	public static final String ICONCLS = "stkos_gear";
	public static final boolean DISABLED = false;

	public StkosMapperModifyMapperNumberMenuItem(
			AbstractStkosTreePanel treePanel, Project project,
			Project accordingProject, TreeNode treeNode) {
		super(treePanel, project, accordingProject, treeNode, TEXT, ICONCLS,
				DISABLED);
	}

	@Override
	public void onClick(BaseItem item, EventObject event) {
		if (accordingProject == null) {
			MessageBox.showAlert("温馨提示", "请选择一个参考本体!");
			return;
		}
		Integer defaultValue = getMapperNumber();
		showDialog(defaultValue);
	}

	protected void showDialog(final Integer defaultValue) {
		WebProtegeDialog
				.showDialog(new WebProtegeOKCancelDialogController<Integer>(
						TEXT) {
					private ExpandingTextBox mapperNumberField;

					@Override
					public Widget getWidget() {
						mapperNumberField = new ExpandingTextBox();
						mapperNumberField.setWidth("450px");
						mapperNumberField.setMultiline(false);
						mapperNumberField.setAnchorVisible(false);
						mapperNumberField.setPlaceholder("请填写新的映射号...");

						if (defaultValue != null) {
							mapperNumberField.setValue(defaultValue.toString());
						}

						setDialogButtonHandler(DialogButton.OK,
								new WebProtegeDialogButtonHandler<Integer>() {
									@Override
									public void handleHide(final Integer data,
											WebProtegeDialogCloser closer) {
										String value = "";
										try {
											value = mapperNumberField
													.getValue().trim();
										} catch (Exception e) {
											GWT.log("Getting mapperNumber error.",
													e);
										}
										if ("".equals(value)) {
											deleteMapperNumber();
										} else {
											if (data == null || data < 0) {
												MessageBox.showAlert("温馨提示",
														"请输入大于等于0的整数");
											} else if (defaultValue == data) {
												MessageBox.showAlert("温馨提示",
														"修改成功!");
												closer.hide();
											} else {
												updateMapperNumber(data);
												closer.hide();
											}
										}
									}
								});
						return mapperNumberField;
					}

					@Override
					public Optional<Focusable> getInitialFocusable() {
						return Optional.<Focusable> of(mapperNumberField);
					}

					@Override
					public Integer getData() {
						Integer data = null;
						try {
							data = Integer.parseInt(mapperNumberField
									.getValue().trim());
						} catch (Exception e) {
						}
						return data;
					}
				});
	}

	protected void updateMapperNumber(int mapperNumber) {
		ProjectId projectId = project.getProjectId();
		String entityName = treeNode.getId();
		UtilEntityType entityType = getEntityType();
		String annotationValue = StkosMapperConstant.STKOSMAPPERVALUEPREFIX
				+ mapperNumber + "; "
				+ UtilConstants.FLAG_REF_PROJECT_DISPLAY_NAME + ":"
				+ accordingProject.getDisplayName() + "; "
				+ UtilConstants.FLAG_REF_PROJECT_ID + ":"
				+ accordingProject.getProjectId().getId();
		AsyncCallback<Void> callback = new AsyncCallback<Void>() {
			@Override
			public void onFailure(Throwable caught) {
				MessageBox.showErrorMessage("修改映射号失败", caught);
			}

			@Override
			public void onSuccess(Void result) {
				MessageBox.showMessage("温馨提示", "修改映射号成功!");
				refreshTreeNode();
			}
		};
		StkosUtilServiceManager.getInstance().updateAnnotaionValue(projectId,
				entityName, entityType, annotaionName, annotationValue,
				callback);
	}
}
